package com.udata.server.model;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.udata.shared.model.UserB;

@PersistenceCapable
public class User {

	public User() {

	}

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key;

	public void setKey(Key key) {
		this.key = key;
	}

	@Persistent
	private String name;

	@Persistent
	private String password;

	@Persistent
	private String email;

	@Persistent
	private Key profileId;

	public Key getKey() {
		return key;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public Key getProfileId() {
		return profileId;
	}

	public void setProfileId(Key profileId) {
		this.profileId = profileId;
	}

	public User(UserB userB) {
		this.update(userB);
	}

	public void update(UserB userB) {
		this.setEmail(userB.getEmail());
		this.setName(userB.getName());
		this.setPassword(userB.getPassword());
		if (userB.getProfileId() != null) {
			this.setProfileId(KeyFactory.stringToKey(userB.getProfileId()));
		}
	}

	public UserB getBean() {
		UserB userB = new UserB();
		if (this.key != null) {
			userB.setKey(KeyFactory.keyToString(this.key));
		}
		userB.setEmail(this.email);
		userB.setName(this.name);
		userB.setPassword(this.password);
		if (this.profileId != null) {
			userB.setProfileId(KeyFactory.keyToString(this.profileId));
		}
		return userB;
	}

}
